package chujisuanfa;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class IsValidSudoku {
    public static boolean isValidSudoku(char[][] board) {
        Map<Character, Integer> row = new HashMap<>(9);
        Map<Character, Integer> mat1 = new HashMap<>(9);
        Map<Character, Integer> mat2 = new HashMap<>(9);
        Map<Character, Integer> mat3 = new HashMap<>(9);
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {

                if (j == 0) {
                    if(i==3||i==6){//矩阵清空
                        mat1.clear();
                        mat2.clear();
                        mat3.clear();
                    }
                    row.clear(); //行清空
                }
                if (board[i][j] == '.') continue;

                if (row.containsKey(board[i][j])) return false;//行不是数独
                row.put(board[i][j],1);

                if(j<3){
                    if(mat1.containsKey(board[i][j])) return false;
                    mat1.put(board[i][j],1);
                }else if(j>5){
                    if(mat2.containsKey(board[i][j])) return false;
                    mat2.put(board[i][j],1);
                }else{
                    if(mat3.containsKey(board[i][j])) return false;
                    mat3.put(board[i][j],1);
                }
            }
        }
        for (int j = 0; j < board[0].length; ++j) {
            for (int i = 0; i < board.length; ++i) {
                if (i == 0) {
                    row.clear(); //列清空
                }
                if (board[i][j] == '.') continue;
                if (row.containsKey(board[i][j])) return false;//列不是数独
                row.put(board[i][j],1);
            }
        }
        return true;
    }

    @Test
    public void test() {
//        System.out.println(isValidSudoku(new char[][]{{'.','.','4','.','.','.','6','3','.'},{'.','.','.','.','.','.','.','.','.'},{'5','.','.','.','.','.','.','9','.'},{'.','.','.','5','6','.','.','.','.'},{'4','.','3','.','.','.','.','.','1'},{'.','.','.','7','.','.','.','.','.'},{'.','.','.','5','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.'},{'.','.','.','.','.','.','.','.','.'}}
//        ));
        int[][] matrix={{1,2,3},{4,5,6},{7,8,8}};
        int[][] copy ;
        copy= Arrays.copyOf(matrix,9);
        copy[0][0]=9;
        System.out.println(Arrays.toString(matrix[0]));
        System.out.println(Arrays.toString(copy[0]));
//        for(int i =0;i<matrix.length;++i){
//            for(int j =0; j<matrix[0].length;++j){
//                matrix[i][j]=copy[matrix.length-1-i][matrix[0].length-1-j];
//            }
//        }
    }
}